package com.apabi.web.controller.api;

import com.apabi.service.ApiMateService;
import com.apabi.service.CitedService;
import com.apabi.service.ReferenceService;
import com.apabi.service.elastic.APICode;
import com.apabi.service.elastic.APIResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by teng on 2018/1/16.
 */
@Controller
@RequestMapping("/api")
public class ReferenceController {

    @Autowired
    private ReferenceService referenceService;

    @Autowired
    private CitedService citedService;

    @Autowired
    private ApiMateService apiMateService;

    /**
     * 一级参考文献获取
     * @param docId
     * @param page
     * @param size
     * @param style
     * @param html
     * @return
     */
    @RequestMapping(value = "/reference/{docId}",method = RequestMethod.GET)
    public @ResponseBody
    APIResult referencePage(@PathVariable String docId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer size,
                            @RequestParam(defaultValue = "GB") String style, @RequestParam(defaultValue = "false") Boolean html)     {
        List<Map<String,String>> list = Lists.newArrayList();
        List<String> refDocId = referenceService.selectReference(docId,page,size);
        for (String ref : refDocId) {
            list.add(apiMateService.selectDoc(ref));
        }
        return  new APIResult(APICode.SUCCESS,list);
    }

    /**
     * 引证文献获取
     * @param docId
     * @param page
     * @param size
     * @param style
     * @param html
     * @return
     */
    @RequestMapping(value = "/cited/{docId}",method = RequestMethod.GET)
    public @ResponseBody
    APIResult citedPage(@PathVariable String docId,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "20") Integer size,
                             @RequestParam(defaultValue = "GB") String style,@RequestParam(defaultValue = "false") Boolean html) {
        List<Map<String,String>> list = Lists.newArrayList();
        List<String> refDocId = citedService.selectCited(docId,page,size);
        for (String ref : refDocId) {
            list.add(apiMateService.selectDoc(ref));
        }
        return  new APIResult(APICode.SUCCESS,list);
    }

    @RequestMapping(path = "/reference/{docId}/common",method = RequestMethod.GET)
    public @ResponseBody
    ObjectNode referenceCommon(@PathVariable String docId,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "20") Integer size,
                             @RequestParam(defaultValue = "GB") String style,@RequestParam(defaultValue = "false") Boolean html) {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.put("code",200);
        rootNode.put("message","测试");
        return rootNode;
    }

    @RequestMapping(value = "/cited/{docId}/common",method = RequestMethod.GET)
    public @ResponseBody
    ObjectNode citedCommon(@PathVariable String docId,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "20") Integer size,
                         @RequestParam(defaultValue = "GB") String style,@RequestParam(defaultValue = "false") Boolean html) {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.put("code",200);
        rootNode.put("message","测试");
        return rootNode;
    }




}
